/**
 * Copyright (c) 2007,中企动力华南研发中心<br>
 * All rights reserved.
 */
package com.ce.pms.charge.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ce.pms.base.service.impl.PmsBaseServiceImpl;
import com.ce.pms.charge.dao.IBusinessCodeDao;
import com.ce.pms.charge.dao.IParameterDefDao;
import com.ce.pms.charge.model.BusinessCodeDomain;
import com.ce.pms.charge.model.ParameterDefDomain;
import com.ce.pms.charge.service.IBusinessCodeService;
import com.ce.pms.charge.vo.BusinessCodeVO;
import com.ce.pms.common.model.SysrefcodeDO;
import com.ce.pms.exception.BusinessException;
import com.sitechasia.webx.core.support.Page;

/**
 * 功能描述：交易码管理<br>
 * @author 罗军林 创建日期：2008-7-31
 */
public class BusinessCodeServiceImpl extends PmsBaseServiceImpl implements IBusinessCodeService{

	private IBusinessCodeDao businessCodeDao;
	private IParameterDefDao parameterDefDao;

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IBusinessCodeService#addBusinessCode(com.ce.pms.charge.vo.BusinessCodeVO)
	 */
	public BusinessCodeVO addBusinessCode(BusinessCodeVO businessCodeVO) {
		checkBusinessCodeVO(businessCodeVO);
		checkBusinessCodeVONotNull(businessCodeVO);
		checkBusinessBizCode(businessCodeVO);

		BusinessCodeDomain businessCodeDomain=new BusinessCodeDomain();
		convertVoToDo(businessCodeVO,businessCodeDomain);
		businessCodeDomain.setSysInfo(this.createInfo(businessCodeVO.getCreator()));

		businessCodeDao.updateObject(businessCodeDomain);

		return convertDoToVO(businessCodeDomain);
	}


	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IBusinessCodeService#deleteBusinessCodeByKeyword(java.lang.Long)
	 */
	public void deleteBusinessCodeByKeyword(Long keyword) {
		checkParameterIsNull(keyword, "charge:businessCodeKeyword_not_null");
		ParameterDefDomain parameterDef=parameterDefDao.findByPkGk(keyword, ParameterDefDomain.GKEY_CHARGE);
		if(parameterDef==null){
			businessCodeDao.deleteObjectById(keyword);
		}else{
			throw new BusinessException("charge:businessCode_is_use",BusinessException.WARNING);
		}
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IBusinessCodeService#editBusinessCode(com.ce.pms.charge.vo.BusinessCodeVO)
	 */
	public BusinessCodeVO editBusinessCode(BusinessCodeVO businessCodeVO) {
		checkBusinessCodeVO(businessCodeVO);
		checkParameterIsNull(businessCodeVO.getKeyword(), "charge:businessCodeKeyword_not_null");
		checkBusinessCodeVONotNull(businessCodeVO);
		checkBusinessBizCode(businessCodeVO);

		BusinessCodeDomain businessCodeDomain = businessCodeDao.findById(businessCodeVO.getKeyword());
		convertVoToDo(businessCodeVO, businessCodeDomain);
		this.modifyInfo(businessCodeDomain.getSysInfo(), businessCodeVO.getCreator());

		return convertDoToVO(businessCodeDomain);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IBusinessCodeService#findBusinessCodeByKeyword(java.lang.Long)
	 */
	public BusinessCodeVO findBusinessCodeByKeyword(Long keyword) {
		BusinessCodeDomain businessCodeDomain=businessCodeDao.findById(keyword);
		return convertDoToVO(businessCodeDomain);
	}
	
	/**
	 * 功能描述:查询交易码Domain  <br>
	 * @param businessId 交易码ID
	 * @return
	 *
	 * Create author:付湘辉 <br>
	 * Create on:2009-1-6  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public BusinessCodeDomain getBusinessCodeDomainById(Long businessId){
		return businessCodeDao.loadById(businessId);
	}

	/* (non-Javadoc)
	 * @see com.ce.pms.charge.service.IBusinessCodeService#queryBusinessCode(com.ce.pms.charge.vo.BusinessCodeVO, int, int)
	 */
	public Page queryBusinessCode(BusinessCodeVO condition, int pageNo, int pageSize) {
		Page page = businessCodeDao.queryBusinessCode(condition, pageNo, pageSize);
		page.setResult( convertDoListTOVoList(page.getResult()) );
		return page;
	}

	/*
	 * (non-Javadoc)
	 * @see com.ce.pms.charge.service.IBusinessCodeService#countBusinessCodeBizCode(com.ce.pms.charge.vo.BusinessCodeVO)
	 */
	public Long countBusinessCodeBizCode(BusinessCodeVO condition) {
		return businessCodeDao.countBusinessCodeBizCode(condition);
	}

	public List<BusinessCodeVO> getDebitNotUseBusinessCodeList(String exceptId) {

		 return businessCodeDao.getDebitNotUseBusinessCodeList(exceptId);
	}

	public List<BusinessCodeVO> getAllNotUseBusinessCodeList(String code) {
		List<BusinessCodeDomain> businessCodeList=businessCodeDao.getAllNotUseBusinessCodeList(code);
		return convertDoListTOVoList(businessCodeList);
	}
	/**
	 * 功能描述: 获取所有收入类型的交易码<br>
	 * @return
	 * Create author:yixianjian <br>
	 * Create on:2008-12-30  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<BusinessCodeVO> getAllIncomeBusinessCode(){
		List<BusinessCodeDomain> businessCodeList = businessCodeDao.getAllIncomeBusinessCode();
		return convertDoListTOVoList(businessCodeList);
	}


	/****私有方法*********************************************************/

	/** 功能描述:VO转DO,注系统信息虽自行设置<br>
	 * @param businessCodeVO
	 * @param businessCodeDomain
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void convertVoToDo(BusinessCodeVO businessCodeVO,BusinessCodeDomain businessCodeDomain){
		businessCodeDomain.setKeyword(businessCodeVO.getKeyword());
		businessCodeDomain.setBizCode(businessCodeVO.getBizCode());
		businessCodeDomain.setDebitOrCredit(businessCodeVO.getDebitOrCredit());
		businessCodeDomain.setDescription(businessCodeVO.getDescription());
		businessCodeDomain.setPayMethod(businessCodeVO.getPayMethod());

		businessCodeDomain.setPayMethod( getSysrefcodeValue(businessCodeVO.getPayMethod()) );
		businessCodeDomain.setDebitOrCredit( getSysrefcodeValue(businessCodeVO.getDebitOrCredit()) );
		checkBusinessType(businessCodeDomain);
	}


	/** 功能描述:<br>
	 * @param businessCodeVO
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String getSysrefcodeValue(String keyword) {
		if(keyword == null ) return null;
		else return this.sysrefcodeDao.findById( Long.valueOf(keyword)).getValue();
	}

	/**
	 * 功能描述:DO转VO，系统信息已经设置<br>
	 * @param businessCodeDomain
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-7-31<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private BusinessCodeVO convertDoToVO(BusinessCodeDomain businessCodeDomain){
		BusinessCodeVO businessCodeVO = new BusinessCodeVO();
		businessCodeVO.setBizCode(businessCodeDomain.getBizCode());
		businessCodeVO.setOldBizCode(businessCodeDomain.getBizCode());
		businessCodeVO.setCreator(businessCodeDomain.getSysInfo().getCreator());
		businessCodeVO.setDescription(businessCodeDomain.getDescription());
		businessCodeVO.setKeyword(businessCodeDomain.getKeyword());

		businessCodeVO.setDebitOrCredit( getDebitOrCreditKeyword(businessCodeDomain) );
		businessCodeVO.setPayMethod( getPayMethodKeyword(businessCodeDomain) );

		businessCodeVO.setDebitOrCreditView( getDebitOrCreditName(businessCodeDomain) );
		businessCodeVO.setPayMethodView( getPayMethodName(businessCodeDomain) );

		return businessCodeVO;
	}

	/**
	 * 功能描述:<br>
	 * @param businessCodeDomain
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String getDebitOrCreditKeyword(BusinessCodeDomain businessCodeDomain){
		SysrefcodeDO debitOrCredit=this.sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.FEE_BUSINESS_CODE_DEBIT_OR_CREDIT_CODE,
				businessCodeDomain.getDebitOrCredit());
		return debitOrCredit == null ? null :	String.valueOf(debitOrCredit.getId());
	}

	/**
	 * 功能描述:<br>
	 * @param businessCodeDomain
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String getPayMethodKeyword(BusinessCodeDomain businessCodeDomain){
		SysrefcodeDO debitOrCredit=this.sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.FEE_BUSINESS_CODE_PAY_METHOD_CODE,
				businessCodeDomain.getPayMethod());
		return debitOrCredit == null ? null :	String.valueOf(debitOrCredit.getId());
	}

	/**
	 * 功能描述:<br>
	 * @param businessCodeDomain
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String getPayMethodName(BusinessCodeDomain businessCodeDomain) {
		SysrefcodeDO debitOrCredit=this.sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.FEE_BUSINESS_CODE_PAY_METHOD_CODE,
				businessCodeDomain.getPayMethod());
		return debitOrCredit == null ? null :	debitOrCredit.getName();

	}

	/** 功能描述:<br>
	 * @param businessCodeDomain
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private String getDebitOrCreditName(BusinessCodeDomain businessCodeDomain) {
		SysrefcodeDO debitOrCredit=this.sysrefcodeDao.getSysrefCodesByCode(
				SysrefcodeDO.FEE_BUSINESS_CODE_DEBIT_OR_CREDIT_CODE,
				businessCodeDomain.getDebitOrCredit());
		return debitOrCredit == null ? null :	debitOrCredit.getName();

	}

	/**
	 * 功能描述:检查必填项是否都填<br>
	 * @param businessCodeVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-1<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkBusinessCodeVONotNull(BusinessCodeVO businessCodeVO) {
		if(businessCodeVO.getBizCode() == null ||
				businessCodeVO.getDescription() == null ){
			throw new BusinessException("common:mustFill_null_exception",BusinessException.ERROR);
		}
	}

	/** 功能描述:<br>
	 * @param businessCodeVO<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-1<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkBusinessCodeVO(BusinessCodeVO businessCodeVO) {
		if(businessCodeVO == null){
			throw new BusinessException("common:unknown_exception",BusinessException.ERROR);
		}
	}

	/**
	 * 功能描述:检查交易码类型，如果交易类型为支付类型，付款方式不能为空，否则将抛出异常<br>
	 * @param businessCodeDomain
	 * Create author:罗军林<br>
	 * Create on:2008-8-1<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkBusinessType(BusinessCodeDomain businessCodeDomain){
		 if(!( (SysrefcodeDO.FEE_BUSINESS_CODE_CREDIT.equals(businessCodeDomain.getDebitOrCredit()) &&
				businessCodeDomain.getPayMethod() != null) ||
				SysrefcodeDO.FEE_BUSINESS_CODE_DEBIT.equals(businessCodeDomain.getDebitOrCredit()))){
			throw new BusinessException("charge_businessType_Not_Null",BusinessException.ERROR);
		}
	}

	/**
	 * 功能描述:检查bizCode的唯一性<br>
	 * @param condition<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-5<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private void checkBusinessBizCode(BusinessCodeVO condition){
		Long result = businessCodeDao.countBusinessCodeBizCode(condition);
		if(result > 0){
			throw new BusinessException("charge:bizCode_exist_exception",BusinessException.ERROR);
		}
	}

	/** 功能描述：do列表转换成VO列表<br>
	 * @param list
	 * @return<br>
	 * Create author:罗军林<br>
	 * Create on:2008-8-1<br>
	 * Edit author:<br>
	 * Edit on:<br>
	 * Why:<br>
	 */
	private List<BusinessCodeVO> convertDoListTOVoList(List<BusinessCodeDomain> list) {
		List<BusinessCodeVO> tempList=new ArrayList<BusinessCodeVO>();
		for(BusinessCodeDomain businessCodeDomain: list){
			tempList.add( convertDoToVO(businessCodeDomain));
		}
		return tempList;
	}


	public List<BusinessCodeVO> getAllCreditBusinessCodeList() {
		return  businessCodeDao.getAllCreditBusinessCodeList();
	}

	/**
	 * @return businessCodeDaogetBusinessCodeDao
	 */
	public IBusinessCodeDao getBusinessCodeDao() {
		return businessCodeDao;
	}

	/**
	 * @param businessCodeDao the businessCodeDao to set
	 */
	public void setBusinessCodeDao(IBusinessCodeDao businessCodeDao) {
		this.businessCodeDao = businessCodeDao;
	}


	public void setParameterDefDao(IParameterDefDao parameterDefDao) {
		this.parameterDefDao = parameterDefDao;
	}








}
